package com.zw.yzk.kabu.mapper;

import com.zw.yzk.kabu.data.network.entity.ExhibitionDetailsEntity;
import com.zw.yzk.kabu.model.ExhibitionDetailsModel;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/3/8.
 */

public class ExhibitionDetailsMapper {

    @Inject
    public ExhibitionDetailsMapper() {
    }

    public ExhibitionDetailsModel transfer(ExhibitionDetailsEntity entity) {
        ExhibitionDetailsModel model = new ExhibitionDetailsModel();
        model.setStartTime(entity.result.startTime);
        model.setCoverImage(entity.result.coverImage);
        model.setLimit(entity.result.limit);
        model.setId(entity.result.id);
        model.setAddress(entity.result.address);
        model.setCollected(entity.result.isCollected);
        model.setDistrict(entity.result.district);
        model.setBuyable(entity.result.isBuyable == 1);
        model.setEndTime(entity.result.endTime);
        model.setProvince(entity.result.province);
        model.setStopPurchaseTime(entity.result.stopPurchaseTime);
        model.setProvince(entity.result.province);
        model.setName(entity.result.name);
        model.setCity(entity.result.city);
        model.setRemark(entity.result.note);
        model.setPropertyList(transferProperty(entity.result.ticketList, entity.result.limit));
        model.setGuestList(transferGuest(entity.result.guestList));

        return model;
    }

    public ExhibitionDetailsModel.Property transferProperty(ExhibitionDetailsEntity.Property property, int limit) {
        ExhibitionDetailsModel.Property p = new ExhibitionDetailsModel.Property();
        p.setContent(property.content);
        p.setExhibitionId(property.exhibitionId);
        p.setId(property.id);
        p.setStock(property.stock);
        p.setDiscountedPrice(property.discountedPrice);
        p.setTotalCount(property.totalCount);
        p.setOriginalPrice(property.originalPrice);
        p.setStock(property.totalCount);
        p.setLimit(Math.min(limit, property.stock));

        return p;
    }

    public List<ExhibitionDetailsModel.Property> transferProperty(List<ExhibitionDetailsEntity.Property> propertyList, int limit) {
        List<ExhibitionDetailsModel.Property> pList = new ArrayList<>();
        if (propertyList == null || propertyList.isEmpty()) {
            return pList;
        }
        for (ExhibitionDetailsEntity.Property property : propertyList) {
            pList.add(transferProperty(property, limit));
        }
        return pList;
    }

    public ExhibitionDetailsModel.Guest transferGuest(ExhibitionDetailsEntity.Guest guest) {
        ExhibitionDetailsModel.Guest g = new ExhibitionDetailsModel.Guest();
        g.setIntroduce(guest.introduce);
        g.setGuestName(guest.guestName);
        g.setImage(guest.image);
        g.setId(guest.id);
        return g;
    }

    public List<ExhibitionDetailsModel.Guest> transferGuest(List<ExhibitionDetailsEntity.Guest> guestList) {
        List<ExhibitionDetailsModel.Guest> gList = new ArrayList<>();
        for (ExhibitionDetailsEntity.Guest guest : guestList) {
            gList.add(transferGuest(guest));
        }
        return gList;
    }
}
